<?php

namespace app\admin\controller\material;

use app\common\controller\Backend;

use think\Controller;
use think\Request;

/**
 * 物料明细管理
 *
 * @icon fa fa-circle-o
 */
class Index extends Backend
{

    /**
     * Material模型对象
     */
    protected $model = null;
    protected $supplierModel = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('material');
        $this->supplierModel = model('supplier');
        $this->view->assign('supplier', $this->supplierModel->field('id,name')->select());
    }

    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->with('supplier')
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    public function add()
    {
        return parent::create('form');
    }

    public function edit($ids = NULL)
    {
        return parent::modify($ids, 'form');
    }

    public function del($ids = null){
        $where = array(
            'id' => array('in',$ids)
        );
        parent::delete($where);
    }
}
